﻿using Ninject;
using QuickChat_Service.Helpers;
using QuickChat_Service.Repositories;
using QuickChat_Service.Repositories.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace QuickChat_Service.Controllers
{
    public class TalkersController : ApiController
    {
        private IChatServiceUnitOfWork UoW{get; set;}

        public TalkersController()
        {
            var kernel = new StandardKernel(new QuickChatDependencyInjectionModule());
            UoW = kernel.Get<IChatServiceUnitOfWork>();

            //UoW = new ChatServiceUnitOfWork();
        }

        public TalkersController(IChatServiceUnitOfWork uoW)
        {
            UoW = uoW;
        }

        // GET api/talkers
        public IQueryable<Talker> Get()
        {
            return UoW.talkerRepo.GetAll();
        }

        // GET api/talkers/5
        public HttpResponseMessage Get(int id)
        {
            var talker = UoW.talkerRepo.GetById(id);

            if(talker == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse<Talker>(HttpStatusCode.OK, talker);  
        }

        public HttpResponseMessage Get(string alias)
        {
            var talker = UoW.talkerRepo.GetByAlias(alias);

            if (talker == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse<Talker>(HttpStatusCode.OK, talker);
        }

        // POST api/talkers
        public HttpResponseMessage Post(Talker talker)
        {
            UoW.talkerRepo.Add(talker);

            if (talker == null || string.IsNullOrWhiteSpace(talker.ChatAlias))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "You must atleat specify a chat alias"));
            }

            var response = Request.CreateResponse<Talker>(HttpStatusCode.Created, talker);
            response.Headers.Location = new Uri(Request.RequestUri.Authority + "/api/Talkers/" + talker.TalkerId.ToString());

            return response;
        }

        // PUT api/talkers/5
        public HttpResponseMessage Put(Talker talker)
        {
            if (talker == null || string.IsNullOrWhiteSpace(talker.ChatAlias) || talker.TalkerId == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "You must atleat specify a chat alias and id"));
            }

            UoW.talkerRepo.Update(talker, talker.TalkerId);
            return Request.CreateResponse(HttpStatusCode.NoContent);
        }

        // DELETE api/talkers/5
        public HttpResponseMessage Delete(int id)
        {
            if (id == null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "You must suppply an id"));
            }

            UoW.talkerRepo.Delete(id);

            return Request.CreateResponse(HttpStatusCode.NoContent);
        }

        public void Put(int id)
        {
            UoW.talkerRepo.LeaveChatRoom(id);
        }

        public void Put(int userId, int roomId)
        {
            UoW.talkerRepo.EnterChatRoom(userId, roomId);
        }
    }
}
